package com.example.util;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.example.common.ConstantConfig;
import com.sun.mail.util.MailSSLSocketFactory;
public class MailUtil {
	
	  
	  
	    /**
	     * 邮件发送器
	     *
	     * @return 配置好的工具
	     */
	    private static synchronized JavaMailSenderImpl  createMailSender() {

	    	
	    	String host =ConstantConfig.getConfig("mail.host");
	 	    Integer port =Integer.parseInt(ConstantConfig.getConfig("mail.port")); 
	 	    String userName =ConstantConfig.getConfig("mail.username"); 
	 	    String password =ConstantConfig.getConfig("mail.password"); 
	 	    
	    	Long timeOut=Long.parseLong(ConstantConfig.getConfig("mail.timeout"));
	    	 
	    	
	        JavaMailSenderImpl sender = new JavaMailSenderImpl();
	        sender.setHost(host);
	        sender.setPort(port);
	        sender.setUsername(userName);
	        sender.setPassword(password);
	        sender.setDefaultEncoding("Utf-8");
	        Properties properties = new Properties();
	        
	        properties.setProperty("mail.transport.protocol", "smtp");
	       // properties.setProperty("mail.smtp.timeout", "1000");//设置链接超时
 	        
 	        
 	        
	        if("true".equals(ConstantConfig.getConfig("mail.ssl.flag")))
	        {
	        	MailSSLSocketFactory sf = null;
				try {
					sf = new MailSSLSocketFactory();
				} catch (GeneralSecurityException e) {
					e.printStackTrace();
				}
				sf.setTrustAllHosts(true);
				properties.put("mail.smtp.ssl.socketFactory", sf);
				properties.put("mail.smtp.ssl.enable", "true");
				properties.setProperty("mail.smtp.auth", "true");//开启认证
	 	       
	        }else
	        {
	        	properties.setProperty("mail.smtp.auth", "false");//不开启认证
	         }
	        
	        if("true".equals(ConstantConfig.getConfig("mail.debug.flag")))
	        {
		        properties.setProperty("mail.debug", "true");//启用调试
	        }else
	        {
		        properties.setProperty("mail.debug", "false");//不启用调试
	        }
	        
	       

	        
	        sender.setJavaMailProperties(properties);
	        return sender;
	    }

	   
	    /** 
	    * 发送邮件
	    * @param toMailAddress  邮件地址
	    * @param title  标题
	    * @param body 主题内容
	    * @throws MessagingException
	    * @throws UnsupportedEncodingException void
	    * @author liuhuaqing
	    * @date 2018年9月14日上午11:46:07
	    */ 
	    public static void sendHtmlMail(String toMailAddress, String title, String body) throws MessagingException,UnsupportedEncodingException {
	        
	    	JavaMailSenderImpl mailSender =createMailSender();
	    	
	    	MimeMessage mimeMessage = mailSender.createMimeMessage();
	        // 设置utf-8或GBK编码，否则邮件会有乱码
	        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
	        String emailFrom =ConstantConfig.getConfig("mail.from");
	        messageHelper.setFrom(emailFrom, "云平台系统");
	        messageHelper.setTo(toMailAddress);
	        messageHelper.setSubject(title);
	        messageHelper.setText(body, true);
	        mailSender.send(mimeMessage);
	    }
}
